package com.work.farm.service.impl;

import com.work.crops.feign.CropsIndexFeign;
import com.work.crops.feign.CropsInfoFeign;
import com.work.crops.pojo.CropsInfoIndex;
import com.work.device.feign.DeviceInfoFeign;
import com.work.device.feign.IndexFeign;
import com.work.device.pojo.DeviceInfo;
import com.work.device.pojo.Index;
import com.work.farm.dao.FarmLandDeviceDao;
import com.work.farm.pojo.TCustFarmlandCropsEntity;
import com.work.farm.pojo.TCustFarmlandDeviceEntity;
import com.work.farm.service.FarmLandDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FarmLandDeviceServiceImpl implements FarmLandDeviceService {

    @Autowired
    private FarmLandDeviceDao farmLandDeviceDao;

    @Autowired
    private CropsIndexFeign cropsIndexFeign;

    @Autowired
    private DeviceInfoFeign deviceInfoFeign;

    @Autowired
    private IndexFeign indexFeign;

    /**
     * 安装设备
     * @param tCustFarmlandDeviceEntity
     */
    @Override
    public void addFarmLandDevice(TCustFarmlandDeviceEntity tCustFarmlandDeviceEntity) {
        farmLandDeviceDao.insertSelective(tCustFarmlandDeviceEntity);
    }

    /**
     * 根据id进行关闭设备
     * @param id
     */
    @Override
    public void closeStatus(Integer id) {
        TCustFarmlandDeviceEntity tCustFarmlandDeviceEntity = farmLandDeviceDao.selectByPrimaryKey(id);
        //设置状态为不可用
        tCustFarmlandDeviceEntity.setStatus("0");
        //更新数据库
        int i = farmLandDeviceDao.updateByPrimaryKeySelective(tCustFarmlandDeviceEntity);

        if (i==0){
            throw new RuntimeException();
        }
    }

    /**
     * 根据id进行设备开启
     * @param id
     */
    @Override
    public void openStatus(Integer id) {
        TCustFarmlandDeviceEntity tCustFarmlandDeviceEntity = farmLandDeviceDao.selectByPrimaryKey(id);
        //设置状态为不可用
        tCustFarmlandDeviceEntity.setStatus("1");
        //更新数据库
        int i = farmLandDeviceDao.updateByPrimaryKeySelective(tCustFarmlandDeviceEntity);

        if (i==0){
            throw new RuntimeException();
        }
    }

    /**
     * 设备自动起停
     * @param id 设备id
     * @param index 监测数据
     * @param stage 生长阶段
     */
    @Override
    public Integer autoOpenAndCloseDevice(Integer id, Integer index,String stage) {
        //根据设备id查询农作物信息
        TCustFarmlandCropsEntity crops = farmLandDeviceDao.findCropsByDeviceId(id);
        //判断大田是否有农作物
        if (crops==null){
            return null;
        }
        //根据农作物id查询对应的农作物详情
        List<CropsInfoIndex> cropsInfoIndexList = cropsIndexFeign.findById(Integer.parseInt(crops.getCid().toString()), stage).getData();
        //遍历,得到每个农作物的相关指标
        for (CropsInfoIndex cropsInfoIndex : cropsInfoIndexList) {
            //获得最大最小值
            String minRange = cropsInfoIndex.getMinRange();
            String maxRange = cropsInfoIndex.getMaxRange();
            //查询数据指标表替换单位
            List<Index> indexList = indexFeign.findAll().getData();

            ArrayList<Object> list = new ArrayList<>();
            for (Index index1 : indexList) {
                if (minRange.contains(index1.getUnit())){
                    //去除数据的单位
                    minRange.replace(index1.getUnit(),"");
                    list.add(minRange);
                }
                if (maxRange.contains(index1.getUnit())){
                    //去除数据的单位
                    maxRange.replace(index1.getUnit(),"");
                    list.add(maxRange);
                }
            }
            //判断是否需要自动开启设备
            if (index>Integer.parseInt(minRange) && index<Integer.parseInt(maxRange)){
                //不需要开启设备
                TCustFarmlandDeviceEntity tCustFarmlandDeviceEntity = farmLandDeviceDao.selectByPrimaryKey(id);
                tCustFarmlandDeviceEntity.setStatus("0");
                int i = farmLandDeviceDao.updateByPrimaryKeySelective(tCustFarmlandDeviceEntity);
                if (i>0){
                    return 30010;
                }
            }else{
                TCustFarmlandDeviceEntity tCustFarmlandDeviceEntity = farmLandDeviceDao.selectByPrimaryKey(id);
                tCustFarmlandDeviceEntity.setStatus("1");
                int i = farmLandDeviceDao.updateByPrimaryKeySelective(tCustFarmlandDeviceEntity);
                if (i>0){
                    return 30020;
                }
            }
        }
        return 0;

    }


}
